﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using T2V.SharePoint.Common.Models;
using T2V.SharePoint.Common.Helpers;
using Microsoft.SharePoint.Utilities;
using System.Web;
using T2V.SharePoint.Common.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections;

namespace T2V.SharePoint.Workflow.Layouts.T2V.SharePoint.Workflow
{
    public partial class EmailApprove : LayoutsPageBase
    {
        public const string KEY = "key";
        protected void Page_Load(object sender, EventArgs e)
        {
            string key = Request[KEY];

            if (string.IsNullOrEmpty(key))
            {
                SecureUpdateParam p = new SecureUpdateParam()
                {
                    ListId = new Guid("2A16FC9B-30F5-4406-B6C3-7A99F24AD35D"),
                    ItemId = 1,
                    Generated = DateTime.Now,
                    UpdateData = new System.Collections.Generic.List<SecureUpdateParam.FieldUpdateParam>()
                    {
                       new  SecureUpdateParam.FieldUpdateParam    () { FieldName="Title", Value="Hooooo" }    ,
                        new  SecureUpdateParam.FieldUpdateParam    () { FieldName="Status", Value="Completed" }  
                    
                    }
                };
                string k = SecureHelper.Encrypt<SecureUpdateParam>(p);

                Response.Redirect(this.Request.RawUrl + "?key=" + HttpUtility.UrlEncode(k));
            };
            //key = HttpUtility.UrlDecode(key);
            SecureUpdateParam param = SecureHelper.Decrypt<SecureUpdateParam>(key);

            UpdateItemWithParam(param);
        }

        private void UpdateItemWithParam(SecureUpdateParam param)
        {
            SPList list = SPContext.Current.Web.Lists[param.ListId];
            var listItem = list.GetItemById(param.ItemId);
            try
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;
            if(listItem.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.WorkflowTask)) {

                Hashtable data = new Hashtable();
                foreach (var item in param.UpdateData)
	            {
                    if(!string.IsNullOrEmpty(item.FieldName))
		            data[item.FieldName] = item.Value;
	            }
                listItem[SPBuiltInFieldId.WorkflowVersion] = 1;
                SPWorkflowTask.AlterTask(listItem, data, true);
            }
            else
            
                foreach (var item in param.UpdateData)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(item.FieldName) && listItem.Fields.ContainsField(item.FieldName))
                        {
                            listItem[item.FieldName] = item.Value;
                            listItem.Update();
                        }
                        else
                        {
                            if(item.FieldId!= Guid.Empty) {
                                listItem[item.FieldName] = item.Value;
                                listItem.Update();
                            }
                            
                        }
                        
                        
                    }
                    catch (Exception ex)
                    {

                        Utility.LogError(ex);
                    }
                }
                
            }
            catch (Exception ex)
            {
            }
            finally
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;
            }
        }
    }
}
